package com.ocean.web.controller.system;

import com.ocean.common.annotation.Log;
import com.ocean.common.core.controller.BaseController;
import com.ocean.common.core.domain.AjaxResult;
import com.ocean.common.core.domain.entity.SysDept;
import com.ocean.common.core.page.TableDataInfo;
import com.ocean.common.enums.BusinessType;
import com.ocean.system.domain.SysFileConfDo;
import com.ocean.system.service.ISysDeptService;
import com.ocean.system.service.ISysFileConfService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: FuGui Wang
 * @Description: 机构储存空间配置 控制层
 * @Date 2023/2/14
 * @Version V1.1.0
 */
@Controller
@RequestMapping("/system/fileconf")
public class SysFileConfController extends BaseController {
    private String prefix = "system/fileconf";

    @Autowired
    private ISysFileConfService sysFileConfService;

    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 机构储存空间配置 页面跳转
     * @return 跳转路径
     */
    @RequiresPermissions("system:fileconf:view")
    @GetMapping()
    public String dept()
    {
        return prefix + "/fileconf";
    }

    @RequiresPermissions("system:fileconf:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysFileConfDo fileConf)
    {
        startPage();
        List<SysFileConfDo> list = sysFileConfService.selectFileconfList(fileConf);
        return getDataTable(list);
    }

    /**
     * 新增 机构储存空间配置 页面跳转
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增 机构储存空间配置
     */
    @RequiresPermissions("system:fileconf:add")
    @Log(title = "机构储存配置管理-新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSaveFileConf(@Validated SysFileConfDo fileConfDo)
    {
        return toAjax(sysFileConfService.addSaveFileConf(fileConfDo));
    }

    /**
     * 修改 机构储存空间配置 页面跳转
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        // TODO 修改组织的展示
        // 根据机构储存配置id查询数据
        SysFileConfDo sysFileConfDo = sysFileConfService.selectFileConfById(id);
        // 将数据返回
        mmap.put("fileConf", sysFileConfDo);
        if(null != sysFileConfDo && null != sysFileConfDo.getDeptId() && sysFileConfDo.getDeptId() > 0){
            SysDept sysDept = sysDeptService.selectDeptById(sysFileConfDo.getDeptId());
            if(null != sysDept){
                sysFileConfDo.setDept(sysDept);
            }
        }
        return prefix + "/edit";
    }

    /**
     * 修改保存用户
     */
    @RequiresPermissions("system:fileconf:edit")
    @Log(title = "机构储存配置管理-修改", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysFileConfDo fileConfDo)
    {
        return toAjax(sysFileConfService.updateFileConf(fileConfDo));
    }

    @RequiresPermissions("system:fileconf:remove")
    @Log(title = "机构储存配置管理-删除", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(sysFileConfService.deleteFileConfByIds(ids));
    }
}
